<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>服务器管理</title>
    <script>
        // 处理服务器列表中的密码显示/隐藏
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('serversTableBody').addEventListener('click', function(e) {
                if (e.target.classList.contains('toggle-password-btn')) {
                    const cell = e.target.closest('.password-cell');
                    const masked = cell.querySelector('.password-masked');
                    const visible = cell.querySelector('.password-visible');
                    const button = e.target;

                    if (masked.style.display !== 'none') {
                        masked.style.display = 'none';
                        visible.style.display = 'inline';
                        button.textContent = '隐藏';
                    } else {
                        masked.style.display = 'inline';
                        visible.style.display = 'none';
                        button.textContent = '显示';
                    }
                }
            });
        });
        // 检查用户登录状态和角色
        document.addEventListener('DOMContentLoaded', function() {
            fetch('/api/check-login')
                .then(response => response.json())
                .then(data => {
                    if (!data.loggedIn) {
                        // 未登录，重定向到登录页面
                        window.location.href = 'login.html';
                    } else if (data.role !== 'admin') {
                        // 不是管理员，提示无权限并跳转回首页
                        alert('您没有权限访问此页面，只有管理员可以访问服务器管理功能');
                        window.location.href = 'index.html';
                    }
                })
                .catch(error => {
                    console.error('检查登录状态失败:', error);
                    alert('检查登录状态失败，请刷新页面重试');
                });
        });
    </script>
    <style>
        /* 全局样式 */
        :root {
            --primary-color: #165DFF;
            --secondary-color: #334155;
            --success-color: #00B42A;
            --danger-color: #F53F3F;
            --warning-color: #FF7D00;
            --light-color: #f7fafc;
            --dark-color: #1a202c;
            --border-color: #e2e8f0;
            --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f8fafc;
            color: var(--dark-color);
            line-height: 1.6;
        }

        /* 容器样式 */
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: var(--shadow);
            transition: var(--transition);
        }

        .container:hover {
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }

        /* 标题样式 */
        h1, h2 {
            color: var(--dark-color);
            margin-bottom: 20px;
            font-weight: 600;
        }

        h1 {
            font-size: 2rem;
            border-bottom: 2px solid var(--primary-color);
            padding-bottom: 10px;
            margin-bottom: 30px;
        }

        h2 {
            font-size: 1.5rem;
            color: var(--primary-color);
            margin-top: 30px;
        }

        /* 表单样式 */
        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--secondary-color);
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            font-size: 1rem;
            transition: var(--transition);
        }

        input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.1);
        }

        /* 按钮样式 */
        button {
            padding: 12px 20px;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
        }

        button[type="submit"] {
            background-color: var(--primary-color);
            color: white;
            width: 100%;
            margin-top: 15px;
        }

        button[type="submit"]:hover {
            background-color: #3a5bdb;
            transform: translateY(-2px);
        }

        .toggle-password-btn {
            width: 60px !important;
            margin-left: 10px !important;
            padding: 4px 8px !important;
            border: 1px solid var(--border-color) !important;
            background-color: var(--light-color) !important;
            color: var(--secondary-color) !important;
            border-radius: 4px !important;
            cursor: pointer !important;
            text-align: center !important;
        }

        .toggle-password-btn:hover {
            background-color: #edf2f7 !important;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }

        th, td {
            padding: 15px;
            text-align: left;
        }

        th {
            background-color: var(--primary-color);
            color: white;
            font-weight: 500;
        }

        tr {
            border-bottom: 1px solid var(--border-color);
            transition: var(--transition);
        }

        tr:last-child {
            border-bottom: none;
        }

        tr:hover {
            background-color: #f8fafc;
            transform: translateX(5px);
        }

        /* 链接样式 */
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
            transition: var(--transition);
            padding: 5px 10px;
            border-radius: 4px;
        }

        .back-link:hover {
            text-decoration: none;
            background-color: rgba(74, 108, 247, 0.1);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }

            .container {
                padding: 15px;
            }

            table {
                display: block;
                overflow-x: auto;
            }
        }
    
    /* 页脚样式 */
    footer {
        text-align: center;
        padding: 20px;
        margin-top: 50px;
        color: var(--secondary-color);
        font-size: 0.9rem;
        border-top: 1px solid var(--border-color);
    }
</style>
</head>
<body>
    <div class="container">
        <a href="index.html" class="back-link">返回首页</a>
        <h1>服务器管理</h1>

        <h2>添加服务器</h2>
        <form id="serverForm">
            <div class="form-group">
                <label for="host">主机地址:</label>
                <input type="text" id="host" name="host" required>
            </div>
            <div class="form-group">
                <label for="port">端口:</label>
                <input type="number" id="port" name="port" value="22">
            </div>
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="description">描述:</label>
                <input type="text" id="description" name="description">
            </div>
            <button type="submit">添加服务器</button>
        </form>

        <h2>服务器列表</h2>
        <table id="serversTable">
            <thead>
                <tr>
                    <th>主机地址</th>
                    <th>端口</th>
                    <th>用户名</th>
                    <th>密码</th>
                    <th>描述</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="serversTableBody">
                <!-- 服务器数据将通过JavaScript动态加载 -->
            </tbody>
        </table>
    </div>

    <script>
        // 加载服务器列表
        function loadServers() {
            fetch('/api/servers')
                .then(response => response.json())
                .then(data => {
                    const tableBody = document.getElementById('serversTableBody');
                    tableBody.innerHTML = '';

                    data.forEach(server => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                            <td>${server.target_host}</td>
                            <td>${server.target_port}</td>
                            <td>${server.username}</td>
                            <td class="password-cell">
                                <span class="password-masked">${'*'.repeat(server.encrypted_pwd.length)}</span>
                                <span class="password-visible" style="display: none;">${server.encrypted_pwd}</span>
                                <button type="button" class="toggle-password-btn" style="width: 60px; margin-left: 10px; padding: 2px 8px; border: 1px solid #ddd; background-color: #f5f5f5; color: #333; border-radius: 4px; cursor: pointer; text-align: center;">显示</button>
                            </td>
                            <td>${server.description || ''}</td>
                            <td>
                                <button type="button" class="delete-server-btn" data-host="${server.target_host}" data-username="${server.username}" style="background-color: var(--danger-color); color: white; padding: 6px 12px; border-radius: 4px; border: none; cursor: pointer;">删除</button>
                            </td>
                        `;
                        tableBody.appendChild(row);
                    });
                })
                .catch(error => console.error('加载服务器列表失败:', error));
        }

        // 表单提交处理
        document.getElementById('serverForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const formData = {
                accountName: document.getElementById('username').value,
                host: document.getElementById('host').value,
                port: document.getElementById('port').value,
                username: document.getElementById('username').value,
                password: document.getElementById('password').value,
                description: document.getElementById('description').value
            };

            fetch('/api/server', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('服务器添加成功!');
                    document.getElementById('serverForm').reset();
                    loadServers(); // 重新加载服务器列表
                } else {
                    alert('添加失败: ' + data.error);
                }
            })
            .catch(error => console.error('添加服务器失败:', error));
        });

        // 页面加载时加载服务器列表
        window.onload = function() {
            loadServers();
            // 为删除按钮添加点击事件
            document.getElementById('serversTableBody').addEventListener('click', function(e) {
                if (e.target.classList.contains('delete-server-btn')) {
                    const host = e.target.getAttribute('data-host');
                    const username = e.target.getAttribute('data-username');
                    
                    if (confirm(`确定要删除服务器 ${host} 上的用户 ${username} 吗？`)) {
                        fetch(`/api/server?host=${encodeURIComponent(host)}&username=${encodeURIComponent(username)}`, {
                            method: 'DELETE'
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                alert('服务器删除成功!');
                                loadServers(); // 重新加载服务器列表
                            } else {
                                alert('删除失败: ' + data.error);
                            }
                        })
                        .catch(error => console.error('删除服务器失败:', error));
                    }
                }
            });
        };
    </script>
    <footer>
        权限管理工具@2025 | version <span id="versionInfo">加载中...</span> | 技术支持 liboi@Servyou.com.cn & wzj@Servyou.com.cn
    </footer>
    <script>
        // 获取并显示版本信息
        document.addEventListener('DOMContentLoaded', function() {
            fetch('/api/version')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('versionInfo').textContent = data.version || '未知版本';
                })
                .catch(error => {
                    console.error('获取版本信息失败:', error);
                    document.getElementById('versionInfo').textContent = '获取失败';
                });
        });
    </script>
</body>
</html>